package com.example.javaweb1.controller;

import com.example.javaweb1.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.thymeleaf.util.StringUtils;

import javax.validation.Valid;

@Controller
public class UserController {


    @GetMapping("/register")
    public String registerForm(Model model) {

        model.addAttribute("user", new User());

        return "register";

    }
    @PostMapping("/register")

    public String greetingSubmit(@Valid @ModelAttribute User user, BindingResult result, Model model) {
        if (result.hasErrors()) {
            model.addAttribute("user",user);
            return "register";
        }
        return "success";

    }
   /* @RequestMapping(value = "/")
    public String index(){
        return "register.html";
    }

    @RequestMapping(value = "/register")
    public String register(@ModelAttribute("form") User user, Model model){
        model.addAttribute("user",user);
        if (!StringUtils.isEmpty(user.getUsername()) && !StringUtils.isEmpty(user.getPassword())){
            return "success";
        }else {
            return "register";
        }
    }*/
}
